package com.gohome.gohomechat.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.gohome.gohomechat.R;
import com.gohome.gohomechat.model.entify.Friend;
import com.gohome.gohomechat.presenter.SelectorContantPresenter;
import com.gohome.gohomechat.presenter.presenterlistener.SelectorContantPresenterInterface;
import com.gohome.gohomechat.ui.adapter.AddressListViewAdapter;
import com.gohome.gohomechat.ui.adapter.SelectorContantListViewAdapter;
import com.gohome.gohomechat.ui.viewlistener.SelectorContantViewInterface;
import com.gohome.gohomechat.utils.ToActivityUtil;
import com.gohome.gohomechat.utils.ToastUtil;
import com.gohome.gohomechat.view.QuickIndexView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/3/11.
 */
public class SelectorContantActivity extends BaseActivity implements SelectorContantViewInterface, QuickIndexView.OnIndexChangedListener, View.OnClickListener, AdapterView.OnItemClickListener {
    private ImageButton selector_imgbtn_back;
    private SelectorContantListViewAdapter adapter;
    private TextView selector_title_save, selector_word;
    private ListView selector_listview;
    private QuickIndexView quickIndexView;
    private List<Friend> datas;
    private String groupname, groupintro;
    private boolean ispublic, isinvitation;
    private SelectorContantPresenterInterface selectorContantPresenterInterface;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 100:
                    //隐藏word
                    selector_word.setVisibility(View.GONE);
                    break;
            }

            return false;
        }
    });

    @Override
    protected int getLayout() {
        return R.layout.activity_selectorcontant;
    }

    @Override
    protected void initPresenter() {
        selectorContantPresenterInterface = new SelectorContantPresenter(this);
    }

    @Override
    protected void initView() {

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            groupname = bundle.getString("groupname");
            groupintro = bundle.getString("groupintro");
            ispublic = bundle.getBoolean("ispublic");
            isinvitation = bundle.getBoolean("isinvitation");
        }

        adapter = new SelectorContantListViewAdapter(this);
        selector_imgbtn_back = findView(R.id.selector_imgbtn_back);
        selector_title_save = findView(R.id.selector_title_save);
        selector_word = findView(R.id.selector_word);
        quickIndexView = findView(R.id.selector_indexview);
        selector_listview = findView(R.id.selector_listview);
    }

    @Override
    protected void initData() {
        quickIndexView.setOnIndexChangedListener(this);
        selector_listview.setAdapter(adapter);
        selector_listview.setOnItemClickListener(this);
        selector_imgbtn_back.setOnClickListener(this);
        selector_title_save.setOnClickListener(this);
        selectorContantPresenterInterface.requestData();
    }

    @Override
    public void onIndexChanged(String word) {
        selector_word.setText(word);
        selector_word.setVisibility(View.VISIBLE);
        //移除未处理的消息
        handler.removeCallbacksAndMessages(null);
        //发延迟消息
        handler.sendEmptyMessageDelayed(100, 1500);

        //滑动listview
        //查找对应的item
        for (int i = 0; i < datas.size(); i++) {
            String fWord = datas.get(i).getPinyin().substring(0, 1).toUpperCase();
            if (word.equals(fWord)) {
                selector_listview.setSelection(i);
                return;
            }
        }
    }

    @Override
    public void onUp() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.selector_imgbtn_back:
                finish();
                break;
            case R.id.selector_title_save:
                selectorContantPresenterInterface.createGroup(groupname, groupintro, ispublic, isinvitation,adapter.getCheck());
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void showDialog() {
        showProgressDialog();
    }

    @Override
    public void dismissDialog() {
        dismissProgressDialog();
    }

    @Override
    public void saveSuccess() {
        ToActivityUtil.startToActivity(this,GroupChatActivity.class);
    }

    @Override
    public void saveFailed() {
        ToastUtil.showToast("组群创建失败");
    }

    @Override
    public void requestSuccess(List<Friend> datas) {
        adapter.addData(datas);
    }
}
